class GestionsController < ApplicationController
  
  before_filter :login_required
  
  def index
    store_location
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    store_location
    @gestion_pages, @gestions = paginate :gestions, :per_page => 10
    
    if params[:page]
      @page = params[:page].to_i
    else
      @page = 1
    end
    
    if @page > 3 then 
      @lnk_from = @page- 3
    else
      @lnk_from = 1
    end
    
    if (@page + 3) <= @gestion_pages.last.number then
      @lnk_to = (@page+ 3)
    else
      @lnk_to = @gestion_pages.last.number
    end

  end

  def show
    @gestion = Gestion.find(params[:id])
  end

  def new
    @gestion = Gestion.new
    
    # preseteo el alumno segun lo que llego por params
    if params[:gestion_alumno] != nil then 
      @gestion.persona_id = params[:gestion_alumno]
    end
    
  end

  def create
    @gestion = Gestion.new(params[:gestion])
    if @gestion.save
      flash[:success] = 'Gestion was successfully created.'
      redirect_back_or_default :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @gestion = Gestion.find(params[:id])
  end

  def update
    @gestion = Gestion.find(params[:id])
    if @gestion.update_attributes(params[:gestion])
      flash[:success] = 'Gestion was successfully updated.'
      redirect_back_or_default :action => 'show', :id => @gestion
    else
      render :action => 'edit'
    end
  end

  def destroy
    Gestion.find(params[:id]).destroy
    redirect_back_or_default :action => 'list'
  end
end
